﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel.DataAnnotations;

namespace CardInChina.Data.Entity.Entity
{
    public partial class Customer
    {
        public Customer Load<T>()
        {
            using (CICEntities db = new CICEntities())
            {
                Customer customer = db.Customers.FirstOrDefault(c => c.CustomerID == this.CustomerID);
                PropertyInfo[] pis = typeof(Customer).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);
                foreach (var p in pis)
                {
                    if (p.PropertyType == typeof(T))
                        p.GetValue(customer, null);
                }
                return customer;
            }
        }

        public string GetDisplayName()
        {
            if (!string.IsNullOrEmpty(DisplayName))
                return DisplayName;
            else
                return Account;
        }

        public string GenerateVerifyCode()
        {
            //sql="update customer set very_code=MD5(cust_id+balance+325.61)"
            return MySqlFunctions.MD5((CustomerID + (double)Balance + 325.61).ToString("F"));
        }

        [NotMapped]
        public bool IsActivated
        {
            get { return IsActived != 0; }
            set { IsActived = (sbyte)(value ? 1 : 0); }
        }

        [NotMapped]
        public bool HasPaypalAccount
        {
            get { return PaypalAccount != null; }
        }

        [NotMapped]
        public bool IsVipCustomer
        {
            get { return IsVip.HasValue ? IsVip.Value : false; }
            set { IsVip = value; }
        }

    }
}
